home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* texture3D.c
- * This program creates a simple 3D texture, and displays it.
- *
- * Escape Key - exit program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid initTexture( GLvoid );
-
- void printHelp( char *progname );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- #define NUMSLICES 256
-
- /* Global Variables */
-
- static GLfloat spin = 1.0;
-
- static unsigned int tex[64][64][64];
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( width / 2, height / 2 );
- glutInitDisplayMode( GLUT_RGBA );
- glutCreateWindow( argv[0] );
-
- initgfx();
- initTexture();
-
- glutKeyboardFunc( keyboard );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates basic 3D texture mapping\n"
- "Escape key - exit the program\n\n",
- progname );
- }
-
- GLvoid
- initgfx( void )
- {
- glClearColor(0.2,0.2,0.5,1.0);
-
- if (!glutExtensionSupported( "GL_EXT_texture3D" )) {
- fprintf(stderr, "GL_EXT_texture3D not supported\n");
- }
-
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- glEnable(GL_BLEND);
- }
-
-
- /* generate a simple 3D texture */
- static void
- make3Dtexture(void)
- {
- int i, j, k;
- unsigned int *p = &tex[0][0][0];
-
- for (i=0; i<64; i++) {
- for (j=0; j<64; j++) {
- for (k=0; k<64; k++) {
- if (i < 10 || i > 48 ||
- j < 10 || j > 48 ||
- k < 10 || k > 48) {
- if (i < 2 || i > 62 ||
- j < 2 || j > 62 ||
- k < 2 || k > 62) {
- *p++ = 0x00000000;
- } else {
- *p++ = 0xff80ffff;
- }
- } else {
- *p++ = 0x000000ff;
- }
- }
- }
- }
- }
-
- GLvoid
- initTexture( GLvoid )
- {
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-
- make3Dtexture();
-
- /* Similar to defining a 2D texture, but note the setting of the
- * wrap parameter for the R coordinate. Also, for 3D textures
- * you probably won't need mipmaps, hence the linear min filter.
- */
- glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_MIN_FILTER,
- GL_LINEAR);
- glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_3D_EXT, GL_TEXTURE_WRAP_R_EXT,
- GL_CLAMP);
- glTexImage3DEXT(GL_TEXTURE_3D_EXT, 0, 4, 64, 64, 64, 0,
- GL_RGBA, GL_UNSIGNED_BYTE, tex);
-
- glEnable(GL_TEXTURE_3D_EXT);
- checkError("initTexture");
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- animate( GLvoid )
- {
- spin = fmodf( spin + 5.0, 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, aspect, 1.0, 100.0 );
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.,0.,-3.0);
- glMatrixMode(GL_TEXTURE);
- }
-
- GLvoid
- drawScene(void)
- {
- int i;
- float r, dr, z, dz;
-
- glColor4f(1, 1, 1, 1.4/NUMSLICES);
- glClear(GL_COLOR_BUFFER_BIT);
-
- /* Display the entire 3D texture by drawing a series of quads that
- * slice through the texture coordinate space. Note that the
- * transformations below are applied to the texture matrix, not the
- * modelview matrix.
- */
- glPushMatrix();
-
- /* center the texture coords around the [0,1] cube */
- glTranslatef(.5,.5,.5);
-
- /* Add a rotation to make the picture more interesting */
- glRotatef(spin,1.,1.,.5);
-
- /* to make sure that the texture coords, after arbitrary rotations,
- * still fully contain the [0,1] cube, make them span a range
- * sqrt(3)=1.74 wide
- */
- r = -0.87; dr = 1.74/NUMSLICES;
- z = -1.00; dz = 2.00/NUMSLICES;
- for (i=0; i < NUMSLICES; i++) {
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord3f(-.87,-.87,r); glVertex3f(-1,-1,z);
- glTexCoord3f(-.87, .87,r); glVertex3f(-1, 1,z);
- glTexCoord3f( .87,-.87,r); glVertex3f( 1,-1,z);
- glTexCoord3f( .87, .87,r); glVertex3f( 1, 1,z);
- glEnd();
- r += dr;
- z += dz;
- }
- glPopMatrix();
- checkError("drawScene");
- glFlush();
- }
-